using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Polymod.Tests
{
    [TestClass]
    public class ModelTreeTests
    {
        //TODO: Add Composition to the class and test the tree!
        public class MyDomainClass
        {
            public int A { get; set; }
            public int B { get; set; }
            public string C;
            public MySubDomainClass Sub { get; set; }
            public IList<MySubDomainClass> SubEnumerable { get; set; }
        }

        public class MySubDomainClass
        {
            public int A { get; set; }
        }

        [TestMethod]
        public void ShouldBuildModel()
        {
            var factory = CreateModelFactory();
            var domain = new MyDomainClass() { A = 10, B = 89, C = "Hello" };
            var o = factory.GetModel(domain);
            Assert.AreEqual(10, o.Get(f => f.A));
            Assert.AreEqual(89, o.Get(f => f.B));
            Assert.AreEqual("Hello", o.Get(f => f.C));
        }

        [TestMethod]
        public void ShouldSetValuesOnModel()
        {
            var factory = CreateModelFactory();
            var domain = new MyDomainClass() { A = 10, B = 89, C = "Hello" };
            var o = factory.GetModel(domain);
            o.Set(f => f.A, 8);
            o.Set(f => f.B, 78);
            o.Set(f => f.C, "Bye");

            Assert.AreEqual(8, domain.A);
            Assert.AreEqual(78, domain.B);
            Assert.AreEqual("Bye", domain.C);
        }

        [TestMethod]
        public void ShouldGetModelForProperty()
        {
            var factory = CreateModelFactory();
            var domain = new MyDomainClass() { A = 10, B = 89, C = "Hello", Sub = new MySubDomainClass(), SubEnumerable = new List<MySubDomainClass>() };
            var o = factory.GetModel(domain);
            o.Get(f => f.Sub).Set(f => f.A, 19);
            Assert.AreEqual(19, domain.Sub.A);
        }

        [TestMethod]
        public void ShouldGetModelForEnumerableProperty()
        {
            var factory = CreateModelFactory();
            var domain = new MyDomainClass() { A = 10, B = 89, C = "Hello", Sub = new MySubDomainClass(), SubEnumerable = new List<MySubDomainClass>() };
            var o = factory.GetModel(domain);
            //o.Get(f => f.SubEnumerable).Add((IModelTree<MySubDomainClass>)factory.GetModel(new MySubDomainClass()));
            var collection = o.GetCollection(f => f.SubEnumerable);
            collection.Add(new MySubDomainClass());
        }

        private ModelFactory CreateModelFactory()
        {
            return new ModelFactory(new TemplateBuilders.ModelTreeTemplateBuilder());
        }

    }
}
